<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Wx extends BaseController {

	public function __construct() {
        parent::__construct();
        // 后台初始化方法
        $this->admin_init();
        // 用户类型判断
        if($this->userType != USER_TYPE_SCHOOL && $this->userType != USER_TYPE_TEACHER) redirect(admin_url("login"));
        // 加载类库
        // $this->load->model('schoolModel');
        $this->load->model('wxAppliModel');
    }

    // 移动端轮播图管理页面
	public function banner() {
        // 生成又拍云签名
        require_once('vendor/autoload.php');
        $serviceConfig = new Upyun\Config(UP_BUCKET, UP_USERNAME, UP_PASSWORD);
        $serviceConfig->setFormApiKey(UP_FORM_API);
        $serviceParams = $serviceConfig->getImageUploadParams(UP_FILEPATH, $this->policy, $this->signature);
        $this->display(ADMIN_DIR. DIRECTORY_SEPARATOR. "wx". DIRECTORY_SEPARATOR. "wx_banner");
    }
   
    // 获取轮播图列表
    public function get_banner_list(){
        // 获取数据条数
        $totalParams['getRowNumber'] = true;
        $totalParams['schoolId'] = $this->rmxSchool['id'];
        $totalParams['campusId'] = $this->campusInfo['id'];
        $totalParams['isDelete'] = 0;
        $total = $this->wxAppliModel->get_banner_list($totalParams);
        // 获取数据
        $data['pageStart'] = $this->get('start');
        $data['pageSize']= $this->get("length");
        $data['schoolId'] = $this->rmxSchool['id'];
        $data['campusId'] = $this->campusInfo['id'];
        $data['isDelete'] = 0;
        $bannerList = $this->wxAppliModel->get_banner_list($data);
        // 返回值
        exit(admin_table_data($total, $bannerList));
    }
    // 保存轮播图
    public function save_banner_action(){
        $bannerId = $this->post('id');
        $bannerImage = $this->post("banner_image");
        $bannerTitle = $this->post('banner_title');
        $bannerSort = $this->post("banner_sort");
        $bannerLink = $this->post("banner_link");
        // 内容检查
        if(!$bannerImage) throw new Exception("请上传幻灯片内容");
        // 入库
        $data['banner_image'] = $bannerImage;
        $data['banner_name'] = $bannerTitle;
        $data['banner_link'] = $bannerLink;
        $data['banner_sort'] = $bannerSort;
        $data['updatetime'] = date("Y-m-d H:i:s");
        if($bannerId) {
            $this->db->update(wxAppliModel::$wxBannerTable, $data, "id='{$bannerId}'");
        } else {
            $data['school_id'] = $this->rmxSchool['id'];
            $data['campus_id'] = $this->campusInfo['id'];
            $data['createtime'] = $data['updatetime'];
            $this->db->insert(wxAppliModel::$wxBannerTable, $data);
        }
        // 返回值
        $msg = $bannerId ? '修改网站轮播图成功' : '添加网站轮播图成功';
        exit(json($msg, true));
    }
    // 删除轮播图
    public function delete_banner_action(){
        $bannerId = $this->post("id");
        // 执行删除操作
        $save['is_delete'] = 1;
        $this->db->update(wxAppliModel::$wxBannerTable, $save, "id='{$bannerId}'");
        exit(json("删除成功", true));
    }
}